package cn.tedu.truetaste.controller;

import cn.tedu.truetaste.pojo.dto.PictureAddNewDTO;
import cn.tedu.truetaste.pojo.dto.PictureUpdateDTO;
import cn.tedu.truetaste.pojo.vo.PictureListItemVO;
import cn.tedu.truetaste.pojo.vo.PictureStandardVO;
import cn.tedu.truetaste.service.IPictureService;
import cn.tedu.truetaste.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理轮播图数据相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "轮播图管理模块")
@Slf4j
@RestController
@RequestMapping("/pictures")
public class PictureController {

    @Autowired
    private IPictureService pictureService;

    public PictureController() {
        log.info("创建控制器对象：PictureController");
    }

    // http://localhost:9989/pictures/add-new
    @ApiOperation("添加轮播图")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理【添加轮播图】的请求，参数：{}", pictureAddNewDTO);
        pictureService.addNew(pictureAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9989/pictures/9527/delete
    @ApiOperation("删除轮播图")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "轮播图id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除轮播图】的请求，参数：{}", id);
        pictureService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9989/pictures/9527/update
    @ApiOperation("修改轮播图详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "轮播图id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, PictureUpdateDTO pictureUpdateDTO) {
        log.debug("开始处理【修改轮播图详情】的请求，参数ID：{}, 新数据：{}", id, pictureUpdateDTO);
        pictureService.updateInfoById(id, pictureUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9989/pictures/9527
    @ApiOperation("根据id查询轮播图详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<PictureStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询轮播图详情】的请求，参数：{}", id);
        PictureStandardVO picture = pictureService.getStandardById(id);
        return JsonResult.ok(picture);
    }

    // http://localhost:9989/pictures
    @ApiOperation("查询轮播图列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<PictureListItemVO>> list() {
        log.debug("开始处理【查询轮播图列表】的请求，无参数");
        List<PictureListItemVO> list = pictureService.list();
        return JsonResult.ok(list);
    }

}
